package com.aaa.marketresearch.webclient.security;

import com.aaa.marketresearch.dataresource.domain.Family;
import com.aaa.marketresearch.dataresource.service.FamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Nail
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private FamilyService familyService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Family family = familyService.getByEmail(username);

        Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
        //roles.add(new SimpleGrantedAuthority(UserRoleEnum.USER.name()));
        roles.add(new SimpleGrantedAuthority("ROLE_" + family.getRole()));

        UserDetails userDetails =
                new org.springframework.security.core.userdetails.User(family.getEmail(),
                        family.getPassword(),
                        roles);

        return userDetails;
    }
}
